﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace DIM_Log
{
    /// <summary>
    /// 日志打印
    /// </summary>
    public class Program_log
    {
        private static readonly string File_path = @"log\Debuglog.log";

        private static SemaphoreSlim _sync = new SemaphoreSlim(1);
        /// <summary>
        /// 异步写入日志文件
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        private static async Task WriteLineAsync(string Ini, string Message)
        {
            if (Message != null)
            {
                await _sync.WaitAsync();

                try
                {
                    File.AppendAllText(File_path, string.Format("【{2}】#[{0}]={1}\r\n", DateTime.Now.ToString("HH:mm:ss.fff"), Message, Ini));
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.ToString());
                }
                finally
                {
                    _sync.Release();
                }

                Debug.WriteLine("{0} {1}", DateTime.Now.ToString("HH:mm:ss.fff"), Message);
            }
        }
        /// <summary>
        /// 正常信息
        /// </summary>
        /// <param name="mess"></param>
        public static void Log_Info(object mess, string title = "系统提示")
        {
            _ = WriteLineAsync("Info", string.Format("{0}:【{1}】", title, mess));
        }
        /// <summary>
        /// 报错信息
        /// </summary>
        /// <param name="mess"></param>
        public static void Log_Error(object mess, string title = "系统异常")
        {
            _ = WriteLineAsync("Error", string.Format("{0}:【{1}】", title, mess));
        }
        /// <summary>
        /// 严重错误信息
        /// </summary>
        /// <param name="mess"></param>
        public static void Log_Fatal(object mess, string title = "系统崩坏")
        {
            _ = WriteLineAsync("Fatal", string.Format("{0}:【{1}】", title, mess));
        }
        /// <summary>
        /// 警告信息
        /// </summary>
        /// <param name="mess"></param>
        public static void Log_Warn(object mess, string title = "系统警告")
        {
            _ = WriteLineAsync("Warn", string.Format("{0}:【{1}】", title, mess));
        }

    }
}
